﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using System.IO;
namespace syc.Sale.Report
{
    public partial class 暂估入库与发票 : System.Web.UI.Page
    {
        protected HSSFWorkbook hssfworkbook;
        protected string 合同量;
        protected string 单价;
        protected string 规格;
        protected string 单位;
        protected DateTime 起始日期, 截止日期;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.NianDu.SelectedValue = DateTime.Today.Year.ToString();
                if (DateTime.Today.Month == 1 || DateTime.Today.Month == 2 || DateTime.Today.Month == 3)
                {
                    this.JiDu.SelectedValue = "1";
                    GetData(this.NianDu.SelectedValue, "1", "3");
                }
                if (DateTime.Today.Month == 4 || DateTime.Today.Month == 5 || DateTime.Today.Month == 6)
                {
                    this.JiDu.SelectedValue = "2";
                    GetData(this.NianDu.SelectedValue, "4", "6");
                }
                if (DateTime.Today.Month == 7 || DateTime.Today.Month == 8 || DateTime.Today.Month == 9)
                {
                    this.JiDu.SelectedValue = "3";
                    GetData(this.NianDu.SelectedValue, "7", "9");
                }
                if (DateTime.Today.Month == 10 || DateTime.Today.Month == 11 || DateTime.Today.Month == 12)
                {
                    this.JiDu.SelectedValue = "4";
                    GetData(this.NianDu.SelectedValue, "10", "12");
                }
                this.Date1.Text = this.起始日期.ToString("yyyy年MM月dd日");
                this.Date2.Text = this.截止日期.ToString("yyyy年MM月dd日");
                this.Date3.Text = this.起始日期.ToString("yyyy年MM月dd日");
                this.Date4.Text = this.截止日期.ToString("yyyy年MM月dd日");
            }
        }
        public void GetData(string yy, string mm1, string mm2)
        {
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            string sql = @"
            declare @date1 smalldatetime
            declare @date2 smalldatetime
            select @date1=月初日期 from 供应处生产月设置 where 年度=@年度 and 月份=@月份1
            select @date2=月末日期 from 供应处生产月设置 where 年度=@年度 and 月份=@月份2
            select @date1,@date2";
            sql = sql.Replace("@年度", "'" + yy + "'");
            sql = sql.Replace("@月份1", "'" + mm1 + "'");
            sql = sql.Replace("@月份2", "'" + mm2 + "'");
            Cmd.CommandText = sql;
            SqlDataReader Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                if (Reader.IsDBNull(0))
                {
                    起始日期 = DateTime.Parse("" + yy + "" + "-01-01");
                }
                else
                {
                    起始日期 = Reader.GetDateTime(0);
                }
                if (Reader.IsDBNull(1))
                {
                    截止日期 = DateTime.Parse("" + yy + "" + "-" + mm2 + "-28");
                }
                else
                {
                    截止日期 = Reader.GetDateTime(1);
                }
            }
            Reader.Close();
            Conn.Close();
        }
        protected void JiDu_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.NianDu.SelectedValue =this.NianDu.Text;
            if (this.JiDu.SelectedValue == "1")
            {
                GetData(this.NianDu.SelectedValue, "1", "3");
            }
            if (this.JiDu.SelectedValue == "2")
            {
                this.JiDu.SelectedValue = "2";
                GetData(this.NianDu.SelectedValue, "4", "6");
            }
            if (this.JiDu.SelectedValue == "3")
            {
                this.JiDu.SelectedValue = "3";
                GetData(this.NianDu.SelectedValue, "7", "9");
            }
            if (this.JiDu.SelectedValue == "4")
            {
                this.JiDu.SelectedValue = "4";
                GetData(this.NianDu.SelectedValue, "10", "12");
            }
            this.Date1.Text = this.起始日期.ToString("yyyy年MM月dd日");
            this.Date2.Text = this.截止日期.ToString("yyyy年MM月dd日");
            this.Date3.Text = this.起始日期.ToString("yyyy年MM月dd日");
            this.Date4.Text = this.截止日期.ToString("yyyy年MM月dd日");
        }

        protected void NianDu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.JiDu.SelectedValue == "1")
            {
                GetData(this.NianDu.SelectedValue, "1", "3");
            }
            if (this.JiDu.SelectedValue == "2")
            {
                this.JiDu.SelectedValue = "2";
                GetData(this.NianDu.SelectedValue, "4", "6");
            }
            if (this.JiDu.SelectedValue == "3")
            {
                this.JiDu.SelectedValue = "3";
                GetData(this.NianDu.SelectedValue, "7", "9");
            }
            if (this.JiDu.SelectedValue == "4")
            {
                this.JiDu.SelectedValue = "4";
                GetData(this.NianDu.SelectedValue, "10", "12");
            }
            this.Date1.Text = this.起始日期.ToString("yyyy年MM月dd日");
            this.Date2.Text = this.截止日期.ToString("yyyy年MM月dd日");
            this.Date3.Text = this.起始日期.ToString("yyyy年MM月dd日");
            this.Date4.Text = this.截止日期.ToString("yyyy年MM月dd日");
        }
       

        protected void GongHuo_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SqlDataSource1.SelectCommand = "SELECT isnull([详细名称],名称) as  名称 FROM [原料供货单位] WHERE ([ID] =" + this.GongHuo.Text + ")";
          
        }
      
        

    }
}
